<?php

namespace app\common\webmodel;


use app\common\model\BaseCache;
use think\Log;
use XznsTools\cache\CacheManage;

/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2021/6/4
 * Time: 2:25 下午
 */

/**
 * Class WebSixOneActive2021
 */
class WebSixOneActive2021 extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.web_six_one_active_2021';
    protected $cacheModel = 'web.six_active_2021';

    /**
     * 获取活动
     * @param $uid
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function activeInfo($uid)
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->getDataAndSet(function () use ($uid)
        {
            try
            {
                $data = (new WebSixOneActive2021())
                    ->field('uid,score,give_num,v1,v2,v3,v4,v5,v6,id')
                    ->where(['uid' => $uid])
                    ->find();
                //初始化阻力用户
                if (!$data)
                {
                    $data = ['uid' => $uid, 'create_time' => time(), 'score' => 0, 'give_num' => 0, 'v1' => 0, 'v2' => 0, 'v3' => 0, 'v4' => 0, 'v5' => 0, 'v6' => 0];
                    //初始阻力用户
                    $data['id'] = $this->insertGetId($data);
                } else
                {
                    $data = $data->toArray();
                }
            }
            catch (\Exception $e)
            {
                $data = [];
            }
            return $data;
        });
    }

    /**
     * 更新用户助力信息
     * @param $uid
     * @param $id
     * @param $data
     * @return WebSixOneActive2021|int
     */
    public function updateData($uid,$id,$data)
    {
        try
        {
            $res = $this->where(['id'=>$id])->update($data);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->setCache($data);
        }
        catch (\Exception $e)
        {
            $res = 0;
            Log::write('高考助力信息更新失败:' . $e->getMessage() . '失败文件信息：【' . $e->getFile() . ':' . $e->getLine() . '】', 'error', true);
        }
        return $res;
    }
}
